//
//  EZSlideView.m
//  AVGirlShow
//
//  Created by 张良 on 2017/10/17.
//  Copyright © 2017年 张良. All rights reserved.
//

#import "EZSlideView.h"

@interface EZSlideView() <UIScrollViewDelegate>

@property(nonatomic,assign) CGSize mainWindow;
//  按钮组所在视图
@property(nonatomic,strong) UIView *buttonsView;
//  焦点线
@property(nonatomic,strong) UIView *lineView;
//  按钮组
@property(nonatomic,strong) NSMutableArray *buttons;
//  内容滑动视图
@property(nonatomic,strong) UIScrollView *contentsScrollView;
//  自动计算按钮的宽度
@property(nonatomic,assign) CGFloat buttonWidth;
          
@end

@implementation EZSlideView

-(instancetype)initWithFrame:(CGRect)frame viewControllers:(NSArray*)viewControllers buttonsTexts:(NSArray*)buttonsTexts normalColor:(UIColor*)normalColor activeColors:(NSArray*)activeColors{
    
    if(self = [super initWithFrame:frame]){
        
        self.mainWindow = [UIScreen mainScreen].bounds.size;
        self.buttons = [NSMutableArray array];
        self.viewControllers = viewControllers;
        self.buttonsTexts = buttonsTexts;
        
        self.activeColors = activeColors;
        self.normalColor = normalColor;
        self.buttonsBackgroundColor = [UIColor whiteColor];
        self.buttonsViewBackgroundColor = [UIColor whiteColor];
        
        self.buttonsFont = [UIFont systemFontOfSize:15];
        self.buttonsViewHeight = 40;
        self.buttonsHeight = 38;
        
        self.lineHeight = 2;
        self.lineMarginTop = 0;
 
        self.bottomLineColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
        self.bottomLineHeight = 1;
        self.bottomLineMarginTop = 1;
        
        self.touchButtonsContentSlideAnimate = NO;
        self.lineHidden = NO;
        self.bottomLineHidden = NO;
        self.switchType = 0;
    }
    return self;
}

-(instancetype) makeView{
    
    [self setupButtons];
    [self setupContents];
    
    return self;
}

//  设置按钮组视图内容区域
-(void) setupButtons{
    
    //  添加按钮所在视图 到自己视图内
    
    //  获取按钮宽度，用屏幕宽度 除以 按钮数量
    self.buttonWidth = self.mainWindow.width / self.viewControllers.count;
    
    //  按钮所在视图
    self.buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mainWindow.width, self.buttonsViewHeight)];
    self.buttonsView.backgroundColor = self.buttonsViewBackgroundColor;
    [self addSubview:self.buttonsView];
    
    //  添加多个按钮 到按钮所在视图
    for (int i = 0; i < self.viewControllers.count; i++) {
        
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * buttonWidth, 0, buttonWidth, self.buttonsHeight)];
        
        //  按钮的宽度为 视图数量 乘 按钮宽度
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake( i * self.buttonWidth, 0, self.buttonWidth, self.buttonsHeight)];
        
        [button setTitle:self.buttonsTexts[i] forState:UIControlStateNormal];
        [button setTitleColor:self.normalColor forState:UIControlStateNormal];
        [button setBackgroundColor:self.buttonsViewBackgroundColor];
        
        //  设置按钮的字体必须要是有 titleLabel.font 这种方式而不能直接 setFont
        button.titleLabel.font = self.buttonsFont;
        
        button.tag = i;
        
        //  如果是第一个按钮，就设置颜色为 首个焦点颜色
        if( i == 0 ){
            [button setTitleColor:self.activeColors[0] forState:UIControlStateNormal];
        }
        
        //  添加按钮点击事件 并将按钮添加到按钮组 和 视图中
        [button addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttons addObject:button];
        [self.buttonsView addSubview:button];
    }
    
    //  如果不隐藏 底部横线 添加横线到 按钮组所在视图
    if(self.bottomLineHidden == NO){
        
        UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonsHeight + self.bottomLineMarginTop, self.mainWindow.width, self.bottomLineHeight)];
        [bottomLine setBackgroundColor:self.bottomLineColor];
        [self.buttonsView addSubview:bottomLine];
    }
    
    //  如果不隐藏焦点线 添加焦点线到 按钮组所在视图
    if(self.lineHidden == NO){
        
        //  线的宽度为0 设置宽度 等于 按钮宽度
        if(self.lineWidth == 0){
            
            self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonsHeight + self.lineMarginTop, self.buttonWidth, self.lineHeight)];
            
        //  设置宽度 并调整 x 值
        }else if(self.lineWidth > 0){
            
            //  按钮宽度 减 焦点线 再除以2 获得平均值
            CGFloat x = (self.buttonWidth - self.lineWidth) / 2;
            self.lineView = [[UIView alloc] initWithFrame:CGRectMake(x, self.buttonsHeight + self.lineMarginTop, self.lineWidth, self.lineHeight)];
        }
        
        //  焦点线默认颜色为 首个焦点颜色
        //  将焦点线 添加到 按钮组所在视图
        [self.lineView setBackgroundColor:self.activeColors[0]];
        [self.buttonsView addSubview:self.lineView];
    }
    
}

//  所有按钮点击事件
-(void) onTouchUp :(id) sender{
    
    UIButton *button = sender;
    //  设置 点击按钮的 标题颜色 和 焦点线的背景颜色
    [button setTitleColor:self.activeColors[button.tag] forState:UIControlStateNormal];
    [self.lineView setBackgroundColor:self.activeColors[button.tag]];
    
    //  设置 其他按钮的 默认颜色
    for (UIButton *button2 in self.buttons) {
        if (button2 != button) {
            [button2 setTitleColor:self.normalColor forState:UIControlStateNormal];
        }
    }
    //  调整内容滚动视图的位置
    [self.contentsScrollView setContentOffset:CGPointMake(button.tag * self.mainWindow.width, 0) animated:self.touchButtonsContentSlideAnimate];
}

//  设置滑动视图内容区域
-(void) setupContents{
    
    //  滑动视图区域 高度为 指定高度 减 按钮组视图高度， y 为按钮组视图高度
    //  将视图添加到 到自己视图内
    self.contentsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.buttonsViewHeight, self.mainWindow.width, self.frame.size.height - self.buttonsViewHeight)];
    [self addSubview:self.contentsScrollView];
    
    //  将多个视图控制器加入到组中
    //  高度为 指定高度 减 按钮组视图高度
    //  将视图控制器的视图 添加到 内容滑动视图
    for (int i = 0; i < self.viewControllers.count; i ++) {
        
        UIViewController * viewController = self.viewControllers[i] ;
        
        viewController.view.frame = CGRectMake(i * self.mainWindow.width, 0, self.mainWindow.width, self.frame.size.height - self.buttonsViewHeight);
        
        [self.contentsScrollView addSubview:viewController.view];
    }
    
    //  设置一大堆属性
    self.contentsScrollView.contentSize = CGSizeMake(self.viewControllers.count * self.mainWindow.width, 0);
    self.contentsScrollView.pagingEnabled = YES;
    self.contentsScrollView.showsHorizontalScrollIndicator = NO;
    self.contentsScrollView.showsVerticalScrollIndicator = NO;
    self.contentsScrollView.directionalLockEnabled = YES;
    self.contentsScrollView.bounces = NO;
    self.contentsScrollView.delegate = self;
}

//  内容滑动视图 滚动事件
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //  获取焦点线平均值宽度空间
    CGFloat space = self.lineWidth == 0 ? 0 : (self.buttonWidth - self.lineWidth) / 2;
    
    //  获取当前页面的值
    int page = scrollView.contentOffset.x / self.mainWindow.width;
    
    //  调整 焦点线的横向位置 x
    CGRect frame = self.lineView.frame;
    frame.origin.x = scrollView.contentOffset.x / self.viewControllers.count + space;
    
//    int currentPage = scrollView.contentOffset.x / self.mainWindow.width;
//    int targetPage = frame.origin.x > [self.buttons[currentPage] frame].origin.x ? currentPage+1 : currentPage-1;
    
    
    //  滑动视图所在的横向位置 x 减 (当前页面数 乘 当前主窗口宽度)
    //  将x值缩小到一屏以内用来计算 颜色渐变切换的 进度
    CGFloat x = scrollView.contentOffset.x - page * self.mainWindow.width;
    
    //  当前视图所在横向位置 除 屏幕宽度 获取小数
    CGFloat progress = x / self.mainWindow.width;
//    NSLog(@"scrollView.x:%f,page*width:%f",scrollView.contentOffset.x,page * [self.buttons[page] frame].size.width);
//    NSLog(@"x:%f,progress:%f",x,progress);
//    [self.buttons[currentPage] setTitleColor:[self transformFromColor:self.activeColors[currentPage] toColor:self.normalColor progress:progress] forState:UIControlStateNormal];
//
//
//    [self.buttons[targetPage] setTitleColor:[self transformFromColor:self.normalColor toColor:self.activeColors[targetPage] progress:progress] forState:UIControlStateNormal];
    
//    if(frame.origin.x > [self.buttons[page] frame].origin.x + frame.size.width / 2){
    
    //  0、带动画 颜色渐变 切换
    if(self.switchType == 0){
        
        //  滑动的时候就 设置焦点线的位置
        self.lineView.frame = frame;
        
        if(frame.origin.x > [self.buttons[page] frame].origin.x){
            
            //  当前不是最后一页
            if(page < self.buttons.count - 1){
                
                //  有焦点线 改变它的颜色
                if(self.lineHidden == NO){
                    
                    self.lineView.backgroundColor = [self transformFromColor:self.activeColors[page] toColor:self.activeColors[page+1] progress:progress] ;
                }
                
                //  改变前面按钮颜色
                [self.buttons[page+1] setTitleColor:[self transformFromColor:self.normalColor toColor:self.activeColors[page+1] progress:progress] forState:UIControlStateNormal];
            }
            
            //  改变当前按钮颜色
            [self.buttons[page] setTitleColor:[self transformFromColor:self.activeColors[page] toColor:self.normalColor progress:progress] forState:UIControlStateNormal];
        }
        
    //  1、带动画 标签滑动到一半时颜色直接 切换
    }else if(self.switchType == 1){
        
        //  滑动的时候就 设置焦点线的位置
        self.lineView.frame = frame;
        
        //  焦点线横向位置 大于 当前按钮横向位置 x + 焦点线宽度 / 2
        //  焦点线横向位置 大于 当前按钮宽度的一半时
        if(frame.origin.x > [self.buttons[page] frame].origin.x + frame.size.width / 2){
            
            //  有焦点线 改变它的颜色
            if(self.lineHidden == NO){
                self.lineView.backgroundColor = self.activeColors[page+1];
            }
            
            //  改变前面按钮颜色
            [self.buttons[page+1] setTitleColor:self.activeColors[page+1] forState:UIControlStateNormal];
            
            //  改变当前按钮颜色
            [self.buttons[page] setTitleColor:self.normalColor forState:UIControlStateNormal];
            
            //  可用for循环改变所有其他按钮颜色...
        }
        else{

            //  有焦点线 改变它的颜色
            if(self.lineHidden == NO){
                self.lineView.backgroundColor = self.activeColors[page];
            }
            
            if(page < self.buttons.count - 1){
                //  改变前面按钮颜色
                [self.buttons[page+1] setTitleColor:self.normalColor forState:UIControlStateNormal];
            }
            //  改变当前按钮颜色
            [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];

            //  可用for循环改变所有其他按钮颜色...
        }
    
    //  2、带动画 滑动到翻页后颜色 切换
    }else if(self.switchType == 2){
        
        //  滑动的时候就 设置焦点线的位置
        self.lineView.frame = frame;
        
        //  焦点线横向位置 等于 按钮的横向位置时
        if( frame.origin.x - space == [self.buttons[page] frame].origin.x){
            
            //  有焦点线 改变它的颜色
            if(self.lineHidden == NO){
                self.lineView.backgroundColor = self.activeColors[page];
            }
            
            //  改变前面按钮颜色
            [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
            
            //  将非当前页面调整颜色，当前页面为激活的颜色
            for (int i = 0; i < self.buttons.count; i++) {
                if(i != page){
                    [self.buttons[i] setTitleColor:self.normalColor forState:UIControlStateNormal];
                }else{
                    [self.buttons[i] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
                }
            }
            
            //  只修改当前页面的前一页、后一页方法，和for方式一致
            
            /*
            //  第一页
            if(page == 0){

                //  改变当前按钮颜色
                [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];

            }else if(page == self.buttons.count -1){

                //  改变当前按钮颜色
                [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];

            //  其他页码
            }else{

                //  改变当前按钮 后一个 颜色
                [self.buttons[page-1] setTitleColor:self.normalColor forState:UIControlStateNormal];

                //  改变当前按钮 前一个 颜色
                [self.buttons[page+1] setTitleColor:self.normalColor forState:UIControlStateNormal];
            }
            */
        }
        
    //  3、不带动画 滑动到翻页后颜色 切换
    }else if(self.switchType == 3){
        
        //  焦点线横向位置 等于 按钮的横向位置时
        if( frame.origin.x - space == [self.buttons[page] frame].origin.x){
            
            //  滑动完成的时候才 设置焦点线的位置
            self.lineView.frame = frame;
            
            //  有焦点线 改变它的颜色
            if(self.lineHidden == NO){
                self.lineView.backgroundColor = self.activeColors[page];
            }
            
            //  改变前面按钮颜色
            [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
            
            //  将非当前页面调整颜色，当前页面为激活的颜色
            for (int i = 0; i < self.buttons.count; i++) {
                if(i != page){
                    [self.buttons[i] setTitleColor:self.normalColor forState:UIControlStateNormal];
                }else{
                    [self.buttons[i] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
                }
            }
        }
    }
//
//    if(frame.origin.x > [self.buttons[page] frame].origin.x){
//
//        self.lineView.backgroundColor = [self transformFromColor:self.activeColors[page] toColor:self.activeColors[page+1] progress:progress] ;
//
//        [self.buttons[page+1] setTitleColor:[self transformFromColor:self.normalColor toColor:self.activeColors[page+1] progress:progress] forState:UIControlStateNormal];
//
//        [self.buttons[page] setTitleColor:[self transformFromColor:self.activeColors[page] toColor:self.normalColor progress:progress] forState:UIControlStateNormal];
//
//
////        for (int i = 0; i < self.buttons.count; i++) {
////            if(i != page+1){
////                [self.buttons[i] setTitleColor:self.normalColor forState:UIControlStateNormal];
////            }
////        }
////    }else if(frame.origin.x < [self.buttons[page] frame].origin.x){
////
////        self.lineView.backgroundColor = [self transformFromColor:self.activeColors[page+1] toColor:self.activeColors[page] progress:scrollView.contentOffset.x / self.mainWindow.width] ;
//
//
////        [self.buttons[page+1] setTitleColor:[self transformFromColor:self.normalColor toColor:self.activeColors[page+1] progress:scrollView.contentOffset.x / self.mainWindow.width] forState:UIControlStateNormal];
////
////        [self.buttons[page] setTitleColor:[self transformFromColor:self.activeColors[page] toColor:self.normalColor progress:scrollView.contentOffset.x / self.mainWindow.width] forState:UIControlStateNormal];
//
//
////        self.lineView.backgroundColor = self.activeColors[page];
////        [self.buttons[page] setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
////
////        for (int i = 0; i < self.buttons.count; i++) {
////            if(i != page){
////                [self.buttons[i] setTitleColor:self.normalColor forState:UIControlStateNormal];
////            }
////        }
//
//    }
    
    
//    NSLog(@"page:%i,line.x:%f,currentButton.x:%f",page,frame.origin.x,[self.buttons[page] frame].origin.x);
    
//    if(frame.origin.x < [self.buttons[page] frame].origin.x + frame.size.width / 2){
//        self.lineView.backgroundColor = self.activeColors[page];
//    }
    
//    NSLog(@"%f,%f,%f",frame.origin.x,[self.buttons[page] frame].origin.x,frame.size.width/ 2);
    
    //  Change button title color
//    for (UIButton * button in self.buttons) {
//
//        if (button.tag == page ) {
//
////            [button setTitleColor:self.activeColors[page] forState:UIControlStateNormal];
//        }else{
//            [button setTitleColor:self.normalColor forState:UIControlStateNormal];
//        }
//    }
}



//  渐变颜色转换方法
- (UIColor *)transformFromColor:(UIColor*)fromColor toColor:(UIColor *)toColor progress:(CGFloat)progress {
    
    if (!fromColor || !toColor) {
//        NSLog(@"Warning !!! color is nil");
        return [UIColor blackColor];
    }
    
    progress = progress >= 1 ? 1 : progress;
    
    progress = progress <= 0 ? 0 : progress;
    
    const CGFloat * fromeComponents = CGColorGetComponents(fromColor.CGColor);
    
    const CGFloat * toComponents = CGColorGetComponents(toColor.CGColor);
    
    size_t  fromColorNumber = CGColorGetNumberOfComponents(fromColor.CGColor);
    size_t  toColorNumber = CGColorGetNumberOfComponents(toColor.CGColor);
    
    if (fromColorNumber == 2) {
        CGFloat white = fromeComponents[0];
        fromColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
        fromeComponents = CGColorGetComponents(fromColor.CGColor);
    }
    
    if (toColorNumber == 2) {
        CGFloat white = toComponents[0];
        toColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
        toComponents = CGColorGetComponents(toColor.CGColor);
    }
    
    CGFloat red = fromeComponents[0]*(1 - progress) + toComponents[0]*progress;
    CGFloat green = fromeComponents[1]*(1 - progress) + toComponents[1]*progress;
    CGFloat blue = fromeComponents[2]*(1 - progress) + toComponents[2]*progress;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}


@end
